package rad.framework.jmx;

import rad.framework.jmx.MBean;
import rad.framework.jmx.JMXUtil;
import java.util.Random;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import rad.framework.jpa.EntityPersistenceContextMap;

import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Startup;

@Name("randomNumber")
@Scope(ScopeType.APPLICATION)
@AutoCreate
@Startup
@MBean(objectName = RandomNumber.NAME)
public class RandomNumber implements RandomNumberMBean {

	public static final String NAME = "rad.framework.jmx:service=RandomNumber";

	private long i;

	public RandomNumber() {
		i = (new Random()).nextInt();
	}

	public long get() {
		// touch something to make sure the context is active...
		EntityPersistenceContextMap.instance();
		return i;
	}

	public void set(long i) {
		this.i = i;
	}

	@PostConstruct
	public void postConstruct() {
	}

	@PreDestroy
	public void preDestroy() {
	}

	public static RandomNumberMBean proxy() {
		return (RandomNumberMBean) JMXUtil.getMBean(RandomNumberMBean.class,
				NAME);
	}

	public static RandomNumberMBean component() {
		return (RandomNumberMBean) Component.getInstance(RandomNumber.class);
	}
}